﻿using System.Collections.ObjectModel;
using System.Collections.Generic;

namespace Rio.Framework.Pages.AppCode.Models
{
    public class DataItemTreeViewCollection : ObservableCollection<DataItemTreeView>
    {
        private DataItemTreeView _root;

        
        
        /// <summary>
        /// Gets or sets the root.
        /// </summary>
        /// <value>The root.</value>
        private DataItemTreeView Root
        {
            get
            {
                return this._root;
            }
            set
            {
                this._root = value;
            }
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="DataItemTreeViewCollection" /> class.
        /// </summary>
        /// <param name="P_owner">The p_owner.</param>
        public DataItemTreeViewCollection(DataItemTreeView P_owner)
        {
            this.Root = P_owner;
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="DataItemTreeViewCollection" /> class.
        /// </summary>
        /// <param name="P_owner">The p_owner.</param>
        /// <param name="collection">The collection.</param>
        public DataItemTreeViewCollection(DataItemTreeView P_owner, IEnumerable<DataItemTreeView> collection)
        : base(collection)
        {
            this.Root = P_owner;
        }

        /// <summary>
        /// Sets the item.
        /// </summary>
        /// <param name="P_iIndex">Index of the p_i.</param>
        /// <param name="P_item">The p_item.</param>
        protected override void SetItem(int P_iIndex, DataItemTreeView P_item)
        {
            P_item.Parent = this.Root;

            base.SetItem(P_iIndex, P_item);
        }

        /// <summary>
        /// Removes all elements from the <see cref="T:System.Collections.ObjectModel.Collection`1" />.
        /// </summary>
        protected override void ClearItems()
        {
            foreach (DataItemTreeView item in this)
            {
                item.Parent = null;
            }
            base.ClearItems();
        }

        /// <summary>
        /// Inserts the item.
        /// </summary>
        /// <param name="P_iIndex">Index of the p_i.</param>
        /// <param name="P_item">The p_item.</param>
        protected override void InsertItem(int P_iIndex, DataItemTreeView P_item)
        {
            P_item.Parent = this.Root;

            base.InsertItem(P_iIndex, P_item);
        }

        /// <summary>
        /// Removes the element at the specified index of the <see cref="T:System.Collections.ObjectModel.Collection`1" />.
        /// </summary>
        /// <param name="P_iIndex"></param>
        /// <exception cref="T:System.ArgumentOutOfRangeException">
        /// <paramref name="index" /> is less than zero.-or-<paramref name="index" /> is equal
        /// to or greater than <see cref="P:System.Collections.ObjectModel.Collection`1.Count" />.
        /// </exception>
        protected override void RemoveItem(int P_iIndex)
        {
            this[P_iIndex].Parent = null;

            base.RemoveItem(P_iIndex);
        }
    }
}
